package cn.bdqn.hospital.controller;

import cn.bdqn.hospital.entity.User;
import cn.bdqn.hospital.service.LoginService;
import cn.bdqn.hospital.service.impl.LoginServiceImpl;
import cn.bdqn.hospital.utils.PrintUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 登录注册
 */
@WebServlet("/login.do")
public class LoginController extends HttpServlet {
    LoginService loginService = new LoginServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try{
            // 路由分配
            String opr = req.getParameter("opr");
            switch (opr){
                case "login":{   // 登录
                    login(req,resp);
                    break;
                }
                case "register":{  // 注册
                    register(req,resp);
                    break;
                }
                default:{
                    throw new ServletException("请求方法不存在！");
                }
            }
        }catch(Exception e){
            e.printStackTrace();
            throw new ServletException(e.getMessage());
        }
    }
    // 登录方法
    public void login(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        // 获取浏览器参数
        String hzName = req.getParameter("hzName");
        String hzPwd = req.getParameter("hzPwd");
        // 用参数调用业务层
        User user = new User();
        user.setHzName(hzName);
        user.setHzPwd(hzPwd);
        boolean result = loginService.login(user);
        // 把业务层的结果返回给浏览器
        req.getSession().setAttribute("hzName",hzName);
        PrintUtils.print(resp,result);
        //记录session
    }

    public void register(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        // 获取浏览器参数
        String hzName = req.getParameter("hzName");
        String hzPwd = req.getParameter("hzPwd");
        String hzSex = req.getParameter("hzSex");
        LocalDateTime birth = LocalDateTime.parse(req.getParameter("birth")+" 00:00:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
      //  Integer hzPhone = Integer.parseInt(req.getParameter("hzPhone"));
        String hzPhone = req.getParameter("hzPhone");
        String shenFen = req.getParameter("shenFen");
        String hzEamil = req.getParameter("hzEamil");
        String hzAddress = req.getParameter("hzAddress");
        // 用参数调用业务层
        User user = new User();
        user.setHzName(hzName);
        user.setHzPwd(hzPwd);
        user.setHzSex(hzSex);
        user.setBirth(birth);
        user.setHzPhone(hzPhone);
        user.setShenFen(shenFen);
        user.setHzEamil(hzEamil);
        user.setHzAddress(hzAddress);
        boolean result = loginService.register(user);
        // 把业务层的结果返回给浏览器
        PrintUtils.print(resp,result);
    }

}
